import request from "@/utils/request";

const GOODS_BASE_URL = "/api/v1/goods";

class GoodsAPI {
  /** 获取系统配置分页数据 */
  static getPage(queryParams?: GoodsPageQuery) {
    return request<any, GoodsPageVO[]>({
      url: `${GOODS_BASE_URL}/page`,
      method: "get",
      params: queryParams,
    });
  }

  static delete(queryParams?: GoodsPageQuery) {
    return request<any, any>({
      url: `${GOODS_BASE_URL}/delete`,
      method: "get",
      params: queryParams,
    });
  }

  static detail(queryParams?: GoodsPageQuery) {
    return request<any, any>({
      url: `${GOODS_BASE_URL}/detail`,
      method: "get",
      params: queryParams,
    });
  }

  static getFormData(id: number) {
    return request<any, GoodsVO>({
      url: `${GOODS_BASE_URL}/${id}/form`,
      method: "get",
    });
  }

  static update(id: number, data: GoodsForm) {
    return request({
      url: `${GOODS_BASE_URL}/${id}`,
      method: "put",
      data: data,
    });
  }

  static save(data: GoodsForm) {
    const param: any = {
      id: data.id,
      code: data.code,
      name: data.name,
      picture: data.picture,
      goodsTypeId: data.goodsType?.id,
    };

    return request({
      url: `${GOODS_BASE_URL}/save`,
      method: "post",
      data: param,
    });
  }

  static getGoodsTypeList(queryParams?: GoodsTypeParam) {
    return request<any, ListItem[]>({
      url: `${GOODS_BASE_URL}/selectGoodsType`,
      method: "get",
      params: queryParams,
    });
  }

  static getGoodsType(goodsTypeId: number) {
    return request<any, GoodsType>({
      url: `${GOODS_BASE_URL}/${goodsTypeId}/formGoodsType`,
      method: "get",
    });
  }
}

export default GoodsAPI;

/** $系统配置分页查询参数 */
export interface GoodsPageQuery extends PageQuery {
  /** 搜索关键字 */
  code?: string;
  id?: number;
}

export interface GoodsPageVO {
  /** 主键 */
  id?: number;
  /** 名称 */
  name?: string;
  /** 编码 */
  code?: string;
  /** 图片 */
  picture?: string;
  goodsTypeId: number;
  goodsTypeName?: string;
}

export interface ListItem extends PageQuery {
  id: number;
  name: string;
}

export interface GoodsForm {
  id?: number;
  code: string;
  name: string;
  picture: string;
  goodsType?: GoodsType;
}

export interface GoodsVO {
  id?: number;
  code: string;
  name: string;
  picture: string;
  goodsTypeId?: number;
}

export interface GoodsType {
  id: number;
  name: string;
}

export interface GoodsTypeParam extends PageQuery {
  id: number;
  name: string;
}
